<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\model\Userdata;

// use \think\Request;
class Index extends Controller {
	public function __construct() {
		parent::__construct();
		if (empty(input('get.tid')) or input('get.tid') == 68) {
			$cate = Db::name('cate')->where('parentid', 68)->select();

			$this->assign('cate', $cate);

		} else {
			$tid = input('get.tid');

			$cate = Db::name('cate')->where('parentid', $tid)->select();

			if (empty($cate)) {
				$cate = Db::name('cate')->where('parentid', 68)->select();
				$this->assign('cate', $cate);

			}
			$this->assign('cate', $cate);

		}
	}
	public function remen() {
		$id = input('post.id');

		$data = Db::name('cate')->where('parentid', $id)->select();
		echo json_encode($data);
	}
	public function index() {
		// var_dump(session('user'));die;
		return view('index');
	}
	public function article() {

		$car = Db::name('car')->where('id', input('get.id'))->find();

		$this->assign('car', $car);
		return $this->fetch();
	}
	public function article_list() {

		return view();
	}
	public function car_list() {
		if (empty(input('get.tid'))) {
			$car = Db::name('car')->where('id', '<', 6)->select();
			$this->assign('car', $car);
			return $this->fetch();
		} else {
			$car = Db::name('car')->where('parentcate', input('get.tid'))->select();
			$this->assign('car', $car);
			return $this->fetch();
		}

	}
	public function car_list_2() {

		return view();
	}
	public function chexing() {

		return view();
	}
	public function liuyan() {

		return view();
	}
	public function location() {

		return view();
	}
	public function page() {

		return $this->fetch();

	}

	public function qiuzhi() {
		$job = input('get.id');
		switch ($job) {
		case '14':
			$job = "市场专员";
			break;
		case '15':
			$job = "百度营销顾问";
			break;
		case '16':
			$job = "UI设计师";
			break;

		}

		$this->assign('job', $job);
		return $this->fetch();
	}
	public function rongyu_list() {

		return view();
	}
	public function zhaopin_list() {

		return view();
	}

	//短信验证
	public function sms() {
		$phone = input('post.phone');

		date_default_timezone_set('PRC');
		$url = 'https://api.miaodiyun.com/20150822/industrySMS/sendSMS';
		$code = mt_rand(1000, 9999);
		$accountSid = '26d3519e6a584269b25b92be9b9087c6';
		$authToken = 'e61cf66d3f1e4d3d946de9bba7e61c63';
		$timestamp = date('YmdHis');
		$data = [
			'accountSid' => $accountSid,
			'smsContent' => "【永航通信】您的验证码为{$code}，请于3分钟内正确输入，如非本人操作，请忽略此短信。",
			'to' => $phone,
			'timestamp' => $timestamp,
			'sig' => md5($accountSid . $authToken . $timestamp),
		];
		$jsonData = '';
		foreach ($data as $key => $value) {
			$jsonData .= $key . '=' . $value . '&';
		}
		$jsonData = rtrim($jsonData, '&');
		$headers = [
			'Content-type:application/x-www-form-urlencoded;charset=UTF-8',
			'Accpet:application/json',
		];
		// 提交请求
		$con = curl_init();
		curl_setopt($con, CURLOPT_URL, $url);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($con, CURLOPT_HEADER, 1);
		curl_setopt($con, CURLOPT_POST, 1);
		curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($con, CURLOPT_POSTFIELDS, $jsonData);
		$result = curl_exec($con);
		curl_close($con);

		echo json_encode(['code' => $code]);

	}

	public function register() {
		include_once 'config.php';
		include_once 'saetv2.ex.class.php';

		$o = new \SaeTOAuthV2(WB_AKEY, WB_SKEY);

		$code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
		$this->assign('code_url', $code_url);
		return view();
	}
	public function login() {
		return view();
	}
	public function register_ok() {
		include_once 'config.php';
		include_once 'saetv2.ex.class.php';

		$o = new \SaeTOAuthV2(WB_AKEY, WB_SKEY);

		if (isset($_REQUEST['code'])) {
			$keys = array();
			$keys['code'] = $_REQUEST['code'];
			$keys['redirect_uri'] = WB_CALLBACK_URL;
			try {
				$token = $o->getAccessToken('code', $keys);
			} catch (OAuthException $e) {
			}
		}

		if ($token) {
			$_SESSION['token'] = $token;
			setcookie('weibojs_' . $o->client_id, http_build_query($token));
			// Db::listen(function($sql){echo $sql;});
			// 存入数据库
			$user = new Userdata;
			$c = new \SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
			$ms  = $c->home_timeline(); // done
			$uid_get = $c->get_uid();
			$uid = $uid_get['uid'];
			$user_message = $c->show_user_by_id( $uid);
			$username = $user_message['name'];
			$user->username = $username;
			$user->email = '417794669@qq.com';
			$user->phone = 18817337723;
			$user -> password = md5('1');
			$result = $user->save();
			// var_dump($result);
			session('user',$username);
			session('uid',$user -> id);
			$_SESSION['user'] = $username;

			return view();
		} else {
			return view('register_no');
		}
	}

	public function register_no() {
		return view();
	}

	public function clear()
	{
		session(null);
		echo null;
	}
}
